package com.sky.web.app;

import com.sky.dto.DishPageDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/dish")
public class AppDishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    // 根据分类id查询菜品列表
    @GetMapping("/list")
    public Result getList(@RequestParam Long categoryId) {
        List<DishVO> voList=null;
        //查询redis
        String dishKey = "DISh:" + categoryId;
        if (redisTemplate.hasKey(dishKey)) {
            voList = (List<DishVO>) redisTemplate.opsForValue().get(dishKey);
            return Result.success(voList);
        }
        // 封装dto条件
        DishPageDTO dishPageDTO = DishPageDTO.builder()
                .categoryId(categoryId)
                .status(1)
                .build();
        voList=dishService.getParamList(dishPageDTO);
        redisTemplate.opsForValue().set(dishKey,voList);
        // 调用service查询,返回结果
        return Result.success(voList);
    }
}
